package com.nit.server.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nit.server.common.QueryPageParam;
import com.nit.server.common.Result;
import com.nit.server.entity.Community;
import com.nit.server.service.CommunityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/community")
public class CommunityController {
    @Autowired
    private CommunityService communityService;

    //新增
    @PostMapping("/add")
    public Result add(@RequestBody Community community){
        return communityService.save(community)?Result.success():Result.fail();
    }
    //修改
    @PostMapping("/update")
    public Result update(@RequestBody Community community){
        return communityService.updateById(community)?Result.success():Result.fail();
    }
    //删除
    @GetMapping("/delete")
    public Result delete(@RequestParam String cId){
        return communityService.removeById(cId)?Result.success():Result.fail();
    }
    @PostMapping("/listPage")
//    public List<User> listPage(@RequestBody HashMap map){
    public Result listPage(@RequestBody QueryPageParam pageParam){
        System.out.println(pageParam);
        String name = (String)pageParam.getParams().get("cname");
        Page<Community> page = new Page<>();
        page.setCurrent(pageParam.getPageNum());
        page.setSize(pageParam.getPageSize());

        LambdaQueryWrapper<Community> lambdaQueryWrapper = new LambdaQueryWrapper();
        if (StringUtils.isNotBlank(name)){
            lambdaQueryWrapper.like(Community::getCName,name);
        }
        IPage result = communityService.pageCC(page,lambdaQueryWrapper);

        return Result.success(result.getTotal(), result.getRecords());
    }
    @GetMapping("/list")
    public Result list(){
        List list = communityService.list();

        return Result.success(list);
    }
    @GetMapping("/cname")
    public Result getcname(String id){
        String s = communityService.getById(id).getCName();

        return Result.success(s);
    }
}
